<template>
    <div id="loader-wrapper">
        <div id="loader"></div>
        <div class="loader-section section-left"></div>
        <div class="loader-section section-right"></div>
        <div class="load_title">正在加载 大迈冷链智慧管理平台,请耐心等待
            <br>
            <span>V1.0</span>
            <img src="./loading.jpg" class="wait-loading-bg">
        </div>
        <Modal
            width="30vw"
            v-model="customerShow"
            title="选择登录公司"
            :mask-closable="false"
            @on-cancel="customerShow = false">
            <el-row style="height: 40px;line-height: 40px;">
                <Select
                    v-model="selectCustomerId"
                    placeholder="请选择"
                    transfer
                    style="width: 100%"
                    filterable>
                    <i-option v-for="item in belongUserCustomerList" :key="item.id" :label="item.customerName" :value="item.id" />
                </Select>
            </el-row>
            <template #footer>
                <el-button :loading="customerConfirmLoading" size="mini" type="primary" @click="confirmSelectCustomer">确认</el-button>
            </template>
        </Modal>
    </div>
</template>

<script>
import api from '@/api/login';
import loginApi from '@/api/login';
import _ from 'lodash';
import md5 from 'js-md5';
import { mapActions } from 'vuex';
export default {
    name: 'cmlLogin',
    data() {
        return {
            belongUserCustomerList: [],
            selectCustomerId: '',
            customerShow: false,
            customerConfirmLoading: false,
            userForm: {
                userName: '',
                password: '',
                captchaCode: '',
                code: '',
                role: null
            }
        };
    },
    methods: {
        ...mapActions('user', [
            'setToken',
            'setUserInfo'
        ]),
        decode(str) {
            return decodeURIComponent(atob(str).split('').map(function (c) {
                return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
            }).join(''));
        },
        cmlLogin(param) {
            api.cmlLogin(param).then(({ data }) => {
                if (data.returnCode === 200) {
                    this.userForm.username = data.returnData.userName;
                    this.userForm.password = data.returnData.password;
                    api.getCustomerByUserName(this.userForm.username).then(async ({ data }) => {
                        if (data.returnCode === 200) {
                            this.belongUserCustomerList = data.returnData;
                            if (this.belongUserCustomerList.length === 0) {
                                this.$Message.error('当前登录用户无所属客户');
                                return;
                            } else if (this.belongUserCustomerList.length === 1) {
                                this.selectCustomerId = this.belongUserCustomerList[0].id
                                this.confirmSelectCustomer();
                            } else {
                                this.selectCustomerId = this.belongUserCustomerList[0].id
                                this.customerShow = true;
                            }
                        } else {
                            this.$Message.error(data.returnMsg);
                        }
                    })
                }
            });
        },
        confirmSelectCustomer() {
            if (!this.selectCustomerId) {
                this.$Message.error('请选择客户');
                return;
            }
            this.userForm.customerIds = [];
            let body = _.cloneDeep(this.userForm);
            body.customerIds.push(this.selectCustomerId);
            // body.password = md5(this.userForm.password).toUpperCase();
            body.loginUserType = 1;
            this.customerConfirmLoading = true;
            api.loginSys(body).then(async ({ data }) => {
                if (data.returnCode === 200) {
                    const token = data.returnData.returnData['id_token'];
                    if (token) {
                        this.setToken(token);
                        let { data } = await api.getUserInfo()
                        if (data.returnCode === 200) {
                            this.setUserInfo(data.returnData);
                            this.$router.push({name: data.returnData.userName === 'customs' ? 'monitor-customer-map' : 'WorkbenchIndex'});
                        } else {
                            this.$Message.error(data.returnMsg);
                        }
                    }
                } else {
                    this.$Message.error(data.returnMsg);
                }
            }).finally(() => {
                this.customerConfirmLoading = false;
            })
        },
    },
    mounted() {
        let param = JSON.parse(this.decode(this.$route.query.key));
        this.cmlLogin(param);
    }
}
</script>

<style scoped>
.chromeframe {
    margin: 0.2em 0;
    background: #ccc;
    color: #000;
    padding: 0.2em 0;
}
#loader-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}
#loader {
    display: block;
    position: relative;
    left: 50%;
    top: 50%;
    width: 150px;
    height: 150px;
    margin: -75px 0 0 -75px;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #FFF;
    -webkit-animation: spin 2s linear infinite;
    -ms-animation: spin 2s linear infinite;
    -moz-animation: spin 2s linear infinite;
    -o-animation: spin 2s linear infinite;
    animation: spin 2s linear infinite;
    z-index: 10;
}
#loader:before {
    content: "";
    position: absolute;
    top: 5px;
    left: 5px;
    right: 5px;
    bottom: 5px;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #FFF;
    -webkit-animation: spin 3s linear infinite;
    -moz-animation: spin 3s linear infinite;
    -o-animation: spin 3s linear infinite;
    -ms-animation: spin 3s linear infinite;
    animation: spin 3s linear infinite;
}
#loader:after {
    content: "";
    position: absolute;
    top: 15px;
    left: 15px;
    right: 15px;
    bottom: 15px;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #656565;
    -moz-animation: spin 1.5s linear infinite;
    -o-animation: spin 1.5s linear infinite;
    -ms-animation: spin 1.5s linear infinite;
    -webkit-animation: spin 1.5s linear infinite;
    animation: spin 1.5s linear infinite;
}
@-webkit-keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
        transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
        -ms-transform: rotate(360deg);
        transform: rotate(360deg);
    }
}
@keyframes spin {
    0% {
        -webkit-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
        transform: rotate(0deg);
    }
    100% {
        -webkit-transform: rotate(360deg);
        -ms-transform: rotate(360deg);
        transform: rotate(360deg);
    }
}
#loader-wrapper .loader-section {
    position: fixed;
    top: 0;
    width: 51%;
    height: 100%;
    background-color: #fff;
    z-index: 0;
    -webkit-transform: translateX(0);
    -ms-transform: translateX(0);
    transform: translateX(0);
}
#loader-wrapper .loader-section.section-left {
    left: 0;
}
#loader-wrapper .loader-section.section-right {
    right: 0;
}
.loaded #loader-wrapper .loader-section.section-left {
    -webkit-transform: translateX(-100%);
    -ms-transform: translateX(-100%);
    transform: translateX(-100%);
    -webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
    transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
}
.loaded #loader-wrapper .loader-section.section-right {
    -webkit-transform: translateX(100%);
    -ms-transform: translateX(100%);
    transform: translateX(100%);
    -webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
    transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
}
.loaded #loader {
    opacity: 0;
    -webkit-transition: all 0.3s ease-out;
    transition: all 0.3s ease-out;
}
.loaded #loader-wrapper {
    visibility: hidden;
    -webkit-transform: translateY(-100%);
    -ms-transform: translateY(-100%);
    transform: translateY(-100%);
    -webkit-transition: all 0.3s 1s ease-out;
    transition: all 0.3s 1s ease-out;
}
.no-js #loader-wrapper {
    display: none;
}
.no-js h1 {
    color: #222222;
}
#loader-wrapper .load_title {
    font-family: '微软雅黑';
    color: #ffffff;
    font-size: 20px;
    width: 100%;
    text-align: center;
    z-index: 0;
    position: absolute;
    top: 60%;
    opacity: 1;
    line-height: 30px;
    text-shadow: 2px 2px 4px #7d7d7d;
    font-weight: bold;
}
#loader-wrapper .load_title span {
    font-style: italic;
    font-size: 13px;
    color: #ffffff;
    opacity: 0.5;
    font-weight: bold;
    font-family: '微软雅黑';
}
.wait-loading-bg {
    position: fixed;
    width: 60%;
    opacity: 0.2;
    bottom: -15%;
    right: 0;
    z-index: -1;
}
</style>
